using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.EventSystems;

public enum BirdState
{
    Waiting,//等待状态
    BeforeShoot,//发射前
    AfterShoot,//发射后
    WaitToDie,//等待死亡
}
public class Bird : MonoBehaviour
{
    public BirdState state = BirdState.BeforeShoot;
    //等待 发射前 发射后
    
    public bool isMouseDown = false;//鼠标按下状态
    public float maxDistance = 2.4f;//最大移动范围
    
    public float flySpeed = 5f;//飞行速度
    
    protected Rigidbody2D rgd;//刚体组件
    
    public bool isFlying = true;//是否碰撞
    public bool isHaveUserdSkill = false;//是否使用过技能
    
    // Start is called before the first frame update
    void Start()
    {
        rgd = GetComponent<Rigidbody2D>();
        rgd.bodyType = RigidbodyType2D.Static;//设置刚体为静态
    }

    // Update is called once per frame
    void Update()
    {
        switch (state)
        {
            case BirdState.Waiting:
                WaitControl();
                break;
            case BirdState.BeforeShoot:
                MoveControl();
                break;
            case BirdState.AfterShoot:
                StopControl();
                SkillControl();
                break;
            case BirdState.WaitToDie:
                break;
            default:
                break;
        }
    }

    private void WaitControl()
    {
        
    }

    private void SkillControl()
    {
        if(isHaveUserdSkill) return;//如果已经使用过技能了就直接返回
        
        //如果它在飞，并且鼠标按下
        if(isFlying && Input.GetMouseButtonDown(0))
        {
            isHaveUserdSkill = true;//当使用了技能就为true
            FlyingSkill();
        }
        
        if(Input.GetMouseButtonDown(0))
        {
            isHaveUserdSkill = true;//当使用了技能就为true
            FullTimeSkill();
        }
    }
    
    //飞行技能(让子类重写实现)
    protected virtual void FlyingSkill()
    {
        
    }
    
    //全时技能(让子类重写实现)
    protected virtual void FullTimeSkill()
    {
        
    }

    private void StopControl()
    {
        //判断如果速度小于0.1f，则停止
        if (rgd.velocity.magnitude < 0.1f)
        {
            Invoke("LoadNextBird", 1f);//延迟1秒加载下一个小鸟
            state = BirdState.WaitToDie;//状态转为等待死亡
            rgd.bodyType = RigidbodyType2D.Static;//设置刚体为静态
        } 
    }
    
    //加载下一个小鸟
    protected void LoadNextBird()
    {
        Vector3 oldPos = transform.position;
        Destroy(gameObject);//销毁当前小鸟
        //播放爆炸动画
        //使用Addressables加载资源
        Addressables.LoadAssetAsync<GameObject>("Boom1").Completed += operation=>
        {
            Instantiate(operation.Result, oldPos, Quaternion.identity);//实例化爆炸动画
        };
        //加载下一个小鸟
        GameManager.Instance.LoadNextBird();
    }

    //鼠标按下
    private void OnMouseDown()
    {
        //按下鼠标，判断鼠标是否在UI上
        if (state == BirdState.BeforeShoot && EventSystem.current.IsPointerOverGameObject() == false)
        {
            isMouseDown = true;
            Slingshot.Instance.StartDraw(transform);
            //播放音效
            AudioManager.Instance.PlayBirdSelect(transform.position);
        }
    }

    //鼠标弹起
    private void OnMouseUp()
    {
        if (state == BirdState.BeforeShoot && EventSystem.current.IsPointerOverGameObject() == false)
        {
            isMouseDown = false;
            Slingshot.Instance.EndDraw();
            Fly();
        }
    }

    //每帧获取移动
    private void MoveControl()
    {
        if (isMouseDown)
        {
            transform.position = GetMousePosition();//获取鼠标位置
            Slingshot.Instance.StartDraw(transform);
        }
    }
    
    //得到鼠标位置
    private Vector3 GetMousePosition()
    {
        Vector3 centerPos = Slingshot.Instance.GetCenterPoint();//获取发射中心点坐标
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);//获取鼠标位置
        mousePosition.z = 0;
        Vector3 mouseDir = mousePosition - centerPos;//鼠标位置-发射中心点=鼠标向量
        
        float distance = mouseDir.magnitude;//获取鼠标向量长度，半径
        
        //判断鼠标点是否超出最大移动范围
        if (distance > maxDistance)
        {
            //鼠标点超出范围，则限制鼠标点在最大移动范围内
            mousePosition = mouseDir.normalized * maxDistance + centerPos;
        }
        
        return mousePosition;
    }

    private void Fly()
    {
        rgd.bodyType = RigidbodyType2D.Dynamic;//设置刚体为动态
        rgd.velocity = (Slingshot.Instance.GetCenterPoint() - transform.position).normalized * flySpeed;//设置飞行速度
        //修改刚体的旋转摩擦力
        rgd.angularDrag = 7f;
        state = BirdState.AfterShoot;//状态转为发射后
        
        AudioManager.Instance.PlayBirdFlying(transform.position);//播放音效
    }

    //去指定位置
    public void GoStage(Vector3 position)
    {
        state = BirdState.BeforeShoot;//状态转为发射前
        transform.position = position;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (state == BirdState.AfterShoot)
        {
            isFlying = true;//设置碰撞了
        }
        //监控碰撞的时候，才播放音效
        if (state == BirdState.AfterShoot && collision.relativeVelocity.magnitude > 5)
        {
            AudioManager.Instance.PlayBirdCollison(transform.position);
        }
    }
}
